<template>
    <div ref="chart" style="width: 600px; height: 400px;"></div>
</template>

<script setup>
import { ref, onMounted, nextTick } from 'vue';
import * as echarts from 'echarts';
import eventBus from '/eventBus';

const chart = ref(null);
const props = defineProps({
    QuestionTypesForProps: {
        type: Array,
        default: () => []
    }
})
let newData = []
let dataAxis = [];

const convertype = () => {
    props.QuestionTypesForProps.map(obj => {
        dataAxis.push(obj.question_type)
        newData.push(obj.total_num)
    });
}
// console.log("props.QuestionTypesForProps",props.QuestionTypesForProps);

onMounted(() => {
    nextTick(() => {
        // console.log("eventBus.state.averageScores", eventBus.state.averageScores);
        const myChart = echarts.init(chart.value);
        convertype()
        let data = newData
        let yMax = 500;
        let dataShadow = [];
        for (let i = 0; i < data.length; i++) {
            dataShadow.push(yMax);
        }
        let option = {
            backgroundColor: '#ecf5ff',

            title: {
                text: '题型数量',
                left: 'center',
                top: 15,
            },
            xAxis: {
                data: dataAxis,
                axisLabel: {
                    inside: true,
                    color: '#fff'
                },
                axisTick: {
                    show: false
                },
                axisLine: {
                    show: false
                },
                z: 10
            },
            yAxis: {
                axisLine: {
                    show: false
                },
                axisTick: {
                    show: false
                },
                axisLabel: {
                    color: '#999'
                }
            },
            dataZoom: [
                {
                    type: 'inside'
                }
            ],
            tooltip: {
                trigger: 'item'
            },
            series: [
                {
                    type: 'bar',
                    showBackground: true,
                    itemStyle: {
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                            { offset: 0, color: '#83bff6' },
                            { offset: 0.5, color: '#188df0' },
                            { offset: 1, color: '#188df0' }
                        ])
                    },
                    emphasis: {
                        itemStyle: {
                            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                { offset: 0, color: '#2378f7' },
                                { offset: 0.7, color: '#2378f7' },
                                { offset: 1, color: '#83bff6' }
                            ])
                        }
                    },
                    data: data
                }
            ]
        };
        // Enable data zoom when user click bar.
        const zoomSize = 6;
        myChart.on('click', function (params) {
            console.log(dataAxis[Math.max(params.dataIndex - zoomSize / 2, 0)]);
            myChart.dispatchAction({
                type: 'dataZoom',
                startValue: dataAxis[Math.max(params.dataIndex - zoomSize / 2, 0)],
                endValue:
                    dataAxis[Math.min(params.dataIndex + zoomSize / 2, data.length - 1)]
            });
        });

        myChart.setOption(option);
    });
});
</script>
